#include <QCoreApplication>
#include <iostream>
#include <QThread>
#include <QVector>
#include <QElapsedTimer>
#include <QObject>
using namespace std;

class Generate : public QThread
{
public:
	Generate()
	{
		cout<<"Generate()"<<endl;
	}
protected:
	void run() override
	{
		cout<<"Generate thread : "<<QThread::currentThread()<<endl;
		e.start();
		for(int i = 0; i < 10; i++)
		{
			v.push_back(qrand() % 1000);
		}
		QThread::msleep(1500);
		qint64 milsec = e.elapsed();
		cout<<"run elapsed time : "<<milsec<<endl;
		this->quick_sort();
	}
	void quick_sort()
	{
		int t;
		e.start();
		for(int i=0; i<10; i++)
		{
			for(int j=0; j<9-i; j++)
			{
				if(v.at(j) > v.at(j+1))
				{
					t = v[j];
					v[j] = v[j+1];
					v[j+1] = t;
				}
			}
		}
		qint64 milsec = e.elapsed();
		cout<<"quick_sort elapsed time : "<<milsec<<endl;
		for(int i=0; i<10; i++)
		{
			cout<<v.at(i)<<"\t";
		}
		cout<<endl;
	}

	QVector<int> v;
	QElapsedTimer e;
};
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	cout<<"Main thread : "<<QThread::currentThread()<<endl;
	Generate g1,g2,g3;
	g1.start();
	g2.start();
	g3.start();
	//退出子线程
	g1.quit();
	g2.quit();
	g3.quit();
	//回收资源
	g1.wait();
	g2.wait();
	g3.wait();
	cout<<endl<<"================"<<endl;
	return a.exec();
}
